from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_jwt.serializers import jwt_decode_handler

from Leisuerly.serializer.label import Labelserializer
from xyz.models import Label, User, Question


# from rest_framework_jwt import


class PutLabelfocusin(APIView):


    def put(self, request, id):
        try:

            token = self.request.auth

            token_user = jwt_decode_handler(token)

            user_id = token_user['user_id']

        except Exception as e:

            return Response(status=404)

        user = User.objects.get(id=user_id)

        label = Label.objects.get(id=id)

        label.users.add(user)

        return Response({'messgae': '关注成功',
                         'success': True})

class PutLabelfocusout(APIView):


    def put(self, request, id):
        try:

            token = self.request.auth

            token_user = jwt_decode_handler(token)

            user_id = token_user['user_id']

        except Exception as e:

            return Response(status=404)

        user = User.objects.get(id=user_id)

        # # 测试语句下面这条
        # user = User.objects.get(id=1)

        label = Label.objects.get(id=id)

        label.users.remove(user)

        return Response({'messgae': '取消关注',
                          'success': True})

class Labeldetails(APIView):

    def get(self, request, id):

        question = Question.objects.get(id=id)

        label = Label.objects.get(id=id)

        question.labels.add(label)

        serializer = Labelserializer(label)

        return Response(serializer.data)